import ssl
import socket
"""
   socker 只能发送、接受http 协议格式的字节。
   
   字节 -decode-> string
   
   string -encode-> 字节
   
"""
if __name__ == '__main__':
    # AF_INET ipv4    SOCK_STREAM tcp
    s = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
    # hs_s = ssl.wrap_socket(s)

    host = 'g.cn'
    s.connect((host, 80))
    request_content = """GET / HTTP1.1\r\nhost:{}\r\n\r\n""".format(host)
    s.send(request_content.encode('UTF-8'))
    buffer_size = 1024
    content = b''
    while True:
        res = s.recv(1024)
        content += res
        if len(res) < buffer_size:
            break
    print(content.decode('UTF-8'))

    # 关闭socket链接
    s.close()
